package com.bjc.nio.c1;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import static com.bjc.nio.util.ByteBufferUtil.debugAll;

/**
 * @program: nettypro
 * @description: 字符串转为bytebuffer
 * @author: bjc
 * @create: 2021-09-15 21:47
 */
public class TestByteBufferString {

    public static void main(String[] args) {
        //1.字符串转为ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put("hello".getBytes());
        debugAll(buffer);

        //2.Charset
        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");
        debugAll(buffer2);

        //3.wrap
        ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
        debugAll(buffer3);

        String str = StandardCharsets.UTF_8.decode(buffer2).toString();
        System.out.println(str);

        buffer.flip();
        String str2 = StandardCharsets.UTF_8.decode(buffer).toString();
        System.out.println(str2);
    }
}
